require("./index.scss");
var STip = ZFT.Util.STip;
var Ajax = ZFT.Util.Ajax;
var Pagination = require("COMMON/modules/pagination-x/v1.0");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Template = {
    tr: ParseTemplate(require("./tpl/tr.xtpl")),
    feelist: ParseTemplate(require("./tpl/feeList.xtpl")),
    modal: ParseTemplate(require("./tpl/confmodal.xtpl")),
    //  baseDom: ParseTemplate(require("./tpl/baseDom.xtpl"))
}

var Main = ZFT.Util.Class({
    container: $("body"),

    init: function () {
        var _this = this,
            pagination;
            _this.pers = ZFT.Util.getPermission();
            _this.getBuildingList();
            _this.getFeeList(1);
            _this.bindClickEvent();
            pagination = _this.pagination = new Pagination({
                container: $(".paginationWrap"),
                count: 5,
                showTotal: true,
                jump: true,
                filter: true
            });

            pagination.on("page.switch", function (toPage, currentPage, totalPage) {
                pagination.render({ current: currentPage, toPage: toPage }),
                    _this.getFeeList(toPage);
            });
        



    },
    EVENTS: {
        "change #feeType": "toggleTable",
        "click .cancel,.close": "closeModal"
    },
    bindClickEvent: function () {
        var _this = this;
        $("#getFeeList").on("click", function () {
            _this.getFeeList(1);
        });
    },
    toggleTable: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var tarType = tarBtn.val();
        if (tarType == "3" || tarType == "4") {
            $("#feeWaterPowerTable").removeClass("z-dis-n").siblings(".g-table").addClass("z-dis-n");
        } else if (tarType == "5" || tarType == "6") {
            $("#feeGtWaterPowerTable").removeClass("z-dis-n").siblings(".g-table").addClass("z-dis-n");
        } else if (tarType == "8") {
            $("#feeOtherTable").removeClass("z-dis-n").siblings(".g-table").addClass("z-dis-n");
        }
        _this.getFeeList(1, tarType);
    },
    getBuildingList: function () {
        var _this = this;
        Ajax("/r/Building_getOwnerBuilding", {
            "type": "POST",
            "dataType": "json",
            success: function (res) {
                if (res.code == 200) {
                    var html,
                        data = res.data;
                    data["approvePer"] = ($.inArray("29", _this.pers) !== -1) ? 1 : 0;
                    html = Template.tr({ data: data});
                    $("#allBuilding").html(html);
                } else {
                    STip("fail", res.msg);
                }
            }
        })
    },
    getFeeList: function (toPage, type) {
        toPage = (!toPage) ? 1 : toPage;
        var _this = this;
        var tarBuilding = $("#allBuilding").val();
        var feeType = $("#feeType").val();
        var tarPage = $(".paginationWrap");
        var pageSize = tarPage.attr("data-size");
        var num = { "3": "10", "4": "10", "5": "7", "6": "7", "8": "8" };
        var provBox = $("#provBox");
        var pers = _this.pers;
        var provBtn = '<button id="passProv" data-id="1" class="m-btn m-btn_primary">审批通过</button>' +
            '<button id="rejectProv" data-id="2" class="m-btn m-btn_ol_default cancel">审批驳回</button>';
        var param = {
            building_id: tarBuilding,
            fee_type: type ? type : feeType,
            page: toPage,
            page_size: pageSize ? pageSize : 15
        };
        var tbody = $("#tbody");
        var btn = ($.inArray("29", pers) !== -1) ? provBtn : "";

        Ajax("/r/PropertyManager_getProp", {
            type: "POST",
            dataType: "json",
            params: param,
            loading: function () {
                tarPage.hide();
                var loading = '<tr><td colspan=' + num[type] + '>正在加载,请稍后...</td></tr>';
                tbody.html(loading);
            },
            success: function (res) {
                if (res.code == 200) {
                    var data = res.data.list,
                        html;
                    // data["type"]=feeType;
                    html = Template.feelist({ data: data });
                    tbody.html(html);
                    _this.pagination.render({ current: toPage, toPage: toPage, total: res.data.total });
                    provBox.html(btn);
                    _this.proveOrNot();
                } else {
                    provBox.html("");
                    tbody.html('<tr><td colspan=' + num[type] + '>' + res.msg + '</td></tr>');
                }
            }
        })
    },
    //费用审批或拒绝
    proveOrNot: function () {
        var _this = this;
        $("#passProv,#rejectProv").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var id = tarBtn.attr("id");
            var type = tarBtn.attr("data-id");
            var modal = $("#modal");
            var feeType = $("#feeType").val();
            var html = Template.modal({ data: type });
            var id_arr = [];
            var checkedId = $("#tbody .u-chkbox_id:checked");
            if ((id === "passProv" && type != "1") || (id === "rejectProv" && type != "2")) return false;
            for (let i = 0, len = checkedId.length; i < len; i++) {
                let id = checkedId.eq(i).attr("data-id");
                id_arr.push(id);
            }
            if (id_arr.length < 1) return STip("fail", "请至少选中一个费用项！");
            modal.html(html);
            $("#confBtn").on("click", function (e) {
                var tarBtn = $(e.currentTarget);
                Ajax("/r/PropertyManager_verifyProp", {
                    type: "POST",
                    dataType: "json",
                    params: {
                        id_arr: JSON.stringify(id_arr),
                        status: type
                    },
                    loading: function () {
                        tarBtn.attr("disabled", "disabled").removeClass("m-btn_primary");
                    },
                    success: function (res) {
                        modal.html("")
                        if (res.code == 200) {
                            STip("success", res.msg);
                            _this.getFeeList(1, feeType);
                        } else {
                            STip("fail", res.msg);
                        }
                    }
                })
            })
        })
    },
    //关闭模态框
    closeModal: function (e) {
        var tarBtn = $(e.currentTarget);
        tarBtn.parents(".m-modal_bg").fadeOut(200);
    }
});

$(function () {
    new Main();
})